home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Paths Regions and Clipping / KeyholeBitmap / KeyholeBitmap.cs next >
Encoding:
Text File  |  2001-01-15  |  1.8 KB  |  61 lines

  1. //--------------------------------------------
  2. // KeyholeBitmap.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Imaging;
  8. using System.Windows.Forms;
  9.  
  10. class KeyholeBitmap: PrintableForm
  11. {
  12.      Bitmap bitmap;
  13.  
  14.      public new static void Main()
  15.      {
  16.           Application.Run(new KeyholeBitmap());
  17.      }
  18.      public KeyholeBitmap()
  19.      {
  20.           Text = "Keyhole Bitmap";
  21.  
  22.                // Load image.
  23.           
  24.           Image image = Image.FromFile(
  25.                "..\\..\\..\\..\\Images and Bitmaps\\Apollo11FullColor.jpg");
  26.  
  27.                // Create clipping path.
  28.  
  29.           GraphicsPath path = new GraphicsPath();
  30.           path.AddArc(80, 0, 80, 80, 45, -270);
  31.           path.AddLine(70, 180, 170, 180);
  32.  
  33.                // Get size of clipping path.
  34.  
  35.           RectangleF rectf = path.GetBounds();
  36.  
  37.                // Create new bitmap initialized to transparent.
  38.  
  39.           bitmap = new Bitmap((int) rectf.Width, (int) rectf.Height, 
  40.                               PixelFormat.Format32bppArgb);
  41.  
  42.                // Create Graphics object based on new bitmap.
  43.  
  44.           Graphics grfx = Graphics.FromImage(bitmap);
  45.  
  46.                // Draw original image on bitmap with clipping.
  47.  
  48.           grfx.SetClip(path);
  49.           grfx.TranslateClip(-rectf.X, -rectf.Y);
  50.           grfx.DrawImage(image, (int) -rectf.X, (int) -rectf.Y,
  51.                                  image.Width, image.Height);
  52.           grfx.Dispose();
  53.      }
  54.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  55.      {
  56.           grfx.DrawImage(bitmap, (cx - bitmap.Width) / 2, 
  57.                                  (cy - bitmap.Height) / 2,
  58.                                  bitmap.Width, bitmap.Height);
  59.      }
  60. }
  61.